import { createRouter, createWebHistory } from "vue-router";

import UserLayout from "@/layouts/UserLayout.vue";
// 用户端组件
// 公共组件（如登录页）
import UserLoginPage from "@/views/user/login/Login.vue";
import HomePage from "@/views/user/home/index.vue";
import CarPage from "@/views/user/car/index.vue";
import DetailPage from "@/views/user/detail/index.vue";
import ProductPage from "@/views/user/product/index.vue";
import OrderPage from "@/views/user/order/index.vue";
import ProfilePage from "@/views/user/profile/index.vue";

import AdminLayout from "@/layouts/AdminLayout.vue";
// 公共组件（如登录页）
import AdminLoginPage from "@/views/admin/login/Login.vue";
import UserManagePage from "@/views/admin/user/index.vue";
import ProdManagePage from "@/views/admin/product/index.vue";
import OrderManagePage from "@/views/admin/order/index.vue";
import CommentManagePage from "@/views/admin/comment/index.vue";
import DataAnalysisManagePage from "@/views/admin/dataAnalysis/index.vue";
import AdminProfilePage from "@/views/admin/profile/index.vue";

const routes = [
  // 公共路由（如登录页）

  // 用户端路由
  {
    path: "/",
    redirect: "/user/home",
  },
  {
    path: "/user/login",
    name: "UserLogin",
    component: UserLoginPage,
  },
  {
    path: "/user",
    component: UserLayout, // 用户端统一布局
    children: [
      {
        path: "home",
        name: "Home",
        component: HomePage,
      },
      {
        path: "product",
        name: "Product",
        component: ProductPage,
      },
      {
        path: "car",
        name: "Car",
        component: CarPage,
      },
      {
        path: "order",
        name: "Order",
        component: OrderPage,
      },
      {
        path: "detail",
        name: "Detail",
        component: DetailPage,
      },
      {
        path: "profile",
        name: "Profile",
        component: ProfilePage,
      },
    ],
  },
  {
    path: "/admin/login",
    name: "Login",
    component: AdminLoginPage,
  },
  // // 管理员端路由
  {
    path: "/admin",
    component: AdminLayout,
    children: [
      {
        path: "user",
        name: "AdminUser",
        component: UserManagePage,
      },
      {
        path: "order",
        name: "AdminOrder",
        component: OrderManagePage,
      },
      {
        path: "comment",
        name: "AdminComment",
        component: CommentManagePage,
      },
      {
        path: "product",
        name: "AdminProduct",
        component: ProdManagePage,
      },
      {
        path: "dataAnalysis",
        name: "AdminDataAnalysis",
        component: DataAnalysisManagePage,
      },
      {
        path: "profile",
        name: "AdminProfile",
        component: AdminProfilePage,
      },
    ],
  },
  // 404 页面
  {
    path: "/:pathMatch(.*)*",
    redirect: "/", // 或指向一个404组件
  },
];

const router = createRouter({
  history: createWebHistory(),
  routes,
});

// 路由守卫：权限控制
router.beforeEach((to, from, next) => {
  const isAuthenticated = localStorage.getItem("token"); // 假设用token判断登录状态
  const userRole = localStorage.getItem("role"); // 假设用户角色存储在localStorage

  // 如果路由需要登录且用户未登录，跳转到登录页
  if (to.meta.requiresAuth && !isAuthenticated) {
    next("/login");
  }
  // 如果路由需要管理员权限但用户不是admin，拒绝访问
  else if (to.meta.role === "admin" && userRole !== "admin") {
    next("/"); // 或跳转到无权限提示页
  }
  // 其他情况放行
  else {
    next();
  }
});

export default router;
